<template>
  <el-main>
    <Navbar></Navbar>
    <!--  条件栏  -->
    <el-row>
      <el-col :span="24">
        <el-input v-model="queryPageBean.queryString" placeholder="编码/名称/助记码" style="width: 200px;"></el-input>
        <el-button @click="findPage(queryPageBean)">查询</el-button>
        <el-button type="primary" @click="handleAddPage()">新建</el-button>
      </el-col>
    </el-row>
    <!--    表格-->
    <Table :table-data="page.records" :table-head="tableHead" :operation="operation"></Table>
    <!--    分页组件-->
    <el-pagination
        align="right"
        @current-change="handleCurrentChange"
        :current-page="queryPageBean.currentPage"
        :page-size="queryPageBean.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="page.total">
    </el-pagination>
    <!--    新建检查项的页面-->
    <el-dialog title="新建检查组" :visible.sync="dialogFormVisible">
<!--   tag开始   -->
      <el-tabs value="first" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-form :model="formData" label-position="top">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="编码" prop="code">
                  <el-input v-model="formData.code"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="名称" prop="name">
                  <el-input v-model="formData.name"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="适用性别" prop="sex">
                  <el-select v-model="formData.sex">
                    <el-option label="不限" value="0"></el-option>
                    <el-option label="男" value="1"></el-option>
                    <el-option label="女" value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="助记码">
                  <el-input v-model="formData.helpCode"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="说明">
                  <el-input v-model="formData.remark" type="textarea"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="注意事项">
                  <el-input v-model="formData.attention" type="textarea"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
<!--   检查项     -->
        <el-tab-pane label="所含检查项" name="second">
          <el-row>
            <el-col :span="24">
              <el-input v-model="queryStringCheckItem" size="mini" placeholder="编码/名称" style="width: 100px;"></el-input>
              <el-button @click="findCheckItems" size="mini" >查询</el-button>
            </el-col>
          </el-row>
          <el-table
              size="mini"
              ref="multipleTable"
              max-height="400"
              :data="checkItems"
              tooltip-effect="dark"
              style="width: 100%"
              @selection-change="handleSelectionChange">
            <el-table-column
                type="selection"
                width="55">
            </el-table-column>
            <el-table-column
                prop="code"
                label="编码">
            </el-table-column>
            <el-table-column
                prop="name"
                label="名称">
            </el-table-column>
            <el-table-column
                prop="sex"
                label="适用性别">
            </el-table-column>
            <el-table-column
                prop="remark"
                label="项目说明">
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>
  </el-main>
</template>

<script>
import Table from "@/components/Table.vue"
import { getCheckGroups, addCheckGroup, getAllCheckItems } from "@/api"

export default {
  data() {
    return {
      tableHead: [],  // 表头
      operation: [],  // 操作
      queryPageBean: {  // 分页查询条件
        currentPage: 1, // 当前页
        pageSize: 10,   // 每页记录数
        queryString: '',// 查询条件
      },
      page: {},       // 后端分页对象
      queryStringCheckItem: '', // 检查项查询条件
      checkItems: [], // 检查项
      dialogFormVisible: false, // 新增框显示
      formData: { // 表单数据
        checkItemIds: [], // 绑定的检查项ID
      },
    }
  },
  watch: {
    // 监听查询条件变化
    'queryPageBean.queryString': {
      handler(newValue, oldValue) {
        this.queryPageBean.currentPage = 1 // 将页数设为1
      },
      immediate: true
    },
  },
  methods: {
    // 检查项选中状态更改时的操作
    handleSelectionChange(val) {
      this.formData.checkItemIds = val.map(({id}) => id)
    },
    // 定义弹出新增页面需要做的事
    handleAddPage() {
      this.findCheckItems() // 查询所有的检查项
      this.dialogFormVisible = true
    },
    // 新增检查组
    handleAdd() {
      addCheckGroup(this.formData).then((res) => {
        if (res.data.flag) {
          this.$message({
            message: res.data.message,
            type: 'success'
          })
          this.findPage()
          this.dialogFormVisible = false
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 查询检查项
    findCheckItems() {
      getAllCheckItems({
        queryString: this.queryStringCheckItem
      }).then((res) => {
        this.checkItems = res.data.data
      })
    },
    // 查询检查组
    findPage(pageQueryBean) {
      getCheckGroups(pageQueryBean ? pageQueryBean : this.queryPageBean).then((res) => {
        this.page = res.data.data
      })
    },
    // 当前页发生变化
    handleCurrentChange(val) {
      this.queryPageBean.currentPage = val
      this.findPage()
    },
  }
  ,
  created() {
    // 自定义操作
    this.operation = [
      {
        type: 'primary',
        name: '编辑',
        action: (row) => {
          console.log(row)
        },
        isDisabled: (row) => {
          return false
        }
      }, {
        type: 'danger',
        name: '删除',
        action: (row) => {
          console.log(row)
        },
        isDisabled: (row) => {
          return false
        }
      }
    ]
    // 自定义表头
    this.tableHead = [
      {
        name: '检查组编码',
        prop: 'code'
      }, {
        name: '检查组名称',
        prop: 'name'
      }, {
        name: '适用性别',
        prop: 'sex',
        diy: (row) => {
          if (row.sex === '1') {
            return '男'
          } else if (row.sex === '0') {
            return '女'
          } else {
            return '不限'
          }
        }
      }, {
        name: '助记码',
        prop: 'helpCode'
      }, {
        name: '说明',
        prop: 'remark'
      }
    ]
    // 获取检查项
    this.findPage()
  }
  ,
  components: {
    Table,
  }
}
</script>

<style lang="less">

</style>